﻿//-----------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 , EricHu. 
//-----------------------------------------------------------------

using System.Text.RegularExpressions;

namespace RDIFramework.Utilities
{
    /// <summary>
    /// 数字通用操作类
    /// 
    /// 修改纪录
    ///     2008-07-01 EricHu 数字通用操作类
    ///     
    /// <author>
    ///     <name>EricHu</name>
    ///     <QQ>80368704</QQ>
    ///     <Email>80368704@qq.com</Email>
    /// </author>
    /// </summary>
    public class MathHelper
    {
        /// <summary>
        /// 检测是否浮点型数据
        /// </summary>
        /// <param name="s">待检查数据</param>
        /// <returns>True:是浮点型，False:不是浮点型</returns>
        public static bool IsDecimal(string s)
        {
            try
            {
                decimal d = decimal.Parse(s);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 检测是否整数型数据
        /// </summary>
        /// <param name="Num">待检查数据</param>
        /// <returns></returns>
        public static bool IsInteger(string Input)
        {
            if (Input == null)
            {
                return false;
            }
            else
            {
                return IsInteger(Input, true);
            }
        }

        /// <summary>
        /// 将输入的字符串转换成整数型数据
        /// </summary>
        /// <param name="strValue">待转换数据</param>
        /// <returns>成功，返回整数型数据；失败，返回null</returns>
        public static int? ToInteger(string strValue)
        {
            if (string.IsNullOrEmpty(strValue.Trim()))
            {
                return null;
            }
            else
            {
                try
                {
                    return int.Parse(strValue.Trim());
                }
                catch
                {
                    return null;
                }
            }
        }

        public static int? StringCastToInteger(object objValue)
        {
            if (objValue == null)
            {
                return null;
            }


            if (string.IsNullOrEmpty(objValue.ToString().Trim()))
            {
                return null;
            }
            else
            {
                try
                {
                    return int.Parse(objValue.ToString().Trim());
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 将输入的字符串转换成浮点型数据
        /// </summary>
        /// <param name="strValue">待转换数据</param>
        /// <returns>成功，返回浮点型数据；失败，返回null</returns>
        public static decimal? ToDecimal(string strValue)
        {
            if (string.IsNullOrEmpty(strValue.Trim()))
            {
                return null;
            }
            else
            {
                try
                {
                    return decimal.Parse(strValue.Trim());
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 是否全是正整数
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public static bool IsInteger(string Input, bool Plus)
        {
            if (Input == null)
            {
                return false;
            }
            else
            {
                string pattern = "^-?[0-9]+$";
                if (Plus)
                    pattern = "^[0-9]+$";
                if (Regex.Match(Input, pattern, RegexOptions.Compiled).Success)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}
